/**
 * glfractal.h
 *
 * Brief Definition of GLFractal
 *
 * This is a simple QGLWidget displaying an openGL Sierpinski Gasket
 */

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include <QTimer>

#include <qgl.h>
#include <iostream>
#include <vector>
#include <time.h>

#include "Vector3.h"
#include "Globals.h"
#include "DebugWriter.h"
#include "Scene.h"
#include "Triangle.h"
#include "Quad.h"
#include "BoundingBox.h"

using namespace std;

const float PI = 3.14159;
const int LOOP_SUBDIVISIONS = 1;
const int REGULAR_SUBDIVISIONS = 1;
const int subdivisions = 0;
const int numRows = 10;
const int numCols = 10;
const float HEIGHT = 1;
const float POOL_FLOOR_HEIGHT = 1;

struct WaterPoint {
	Vector3 p; //point in 3D space
	bool up; //whether it is currently moving up or down
	bool control; //whether it is a control point (or a subdivided point)
	float ratio; //how far it will move before changing direction
};

class GLWidget : public QGLWidget
{
  Q_OBJECT
public:
  GLWidget( QWidget* parent);
  ~GLWidget();

  void initialize(Scene* s);
  void update(float seconds, bool draw);

 public slots:
	void btnOpenGL();

protected:
    void initializeGL();
    void paintGL();
    void resizeGL( int w, int h );

private:
	void subdivide(const Vector3& p00, const Vector3& p01, const Vector3& p11, const Vector3& p10, vector<vector<Vector3> >& newPoints);
	void loopSubdivision(int levels, vector<vector<Vector3> >& inPoints, vector<vector<Vector3> >& subdividedPoints);
	void regularSubdivision(int levels, vector<vector<Vector3> >& inPoints, vector<vector<Vector3> >& subdividedPoints);
	void createCausticTriangles(vector<vector<Vector3> >& subdividedPoints);
	Vector3 transmit(Vector3 d, Vector3 n, float nValue);

	vector<vector<WaterPoint> > waterPoints;
	vector<vector<Vector3> > faceNormals;
	vector<vector<Vector3> > vertexNormals;
	int rows, cols;
	clock_t endWait;
	bool initialized;
	DebugWriter debug;
	bool openGL;
	Scene* scene;
};

#endif // GLWIDGET_H
